
# 卡池管理相关视图
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect, get_object_or_404
from LogisticsCard.models import CardPool, PoolCardRelation, CardDeactivation, Cards


@login_required
def pool_list_view(request):
    """
    卡池列表视图，展示所有卡池信息。
    :param request: HttpRequest对象
    :return: 渲染卡池列表页面
    """
    pools = CardPool.objects.filter(delete_at__isnull=True).order_by('-created_at')
    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    paginator = Paginator(pools, 10)
    page = request.GET.get('page')
    try:
        page_obj = paginator.page(page)
    except PageNotAnInteger:
        page_obj = paginator.page(1)
    except EmptyPage:
        page_obj = paginator.page(paginator.num_pages)
    return render(request, 'pool/index.html', {'page_obj': page_obj})


@login_required
def pool_detail_view(request, pool_id):
    """
    卡池详情视图，展示特定卡池的信息及其关联的卡。
    :param request: HttpRequest对象
    :param pool_id: 卡池的ID
    :return: 渲染卡池详情页面
    """
    pool = get_object_or_404(CardPool, id=pool_id, delete_at__isnull=True)
    relations = PoolCardRelation.objects.filter(pool=pool, delete_at__isnull=True)
    return render(request, 'pool/detail.html', {'pool': pool, 'relations': relations})


@login_required
def deactivation_list_view(request):
    """
    卡失效列表视图，展示所有失效卡的信息。
    :param request: HttpRequest对象
    :return: 渲染卡失效列表页面
    """
    deactivations = CardDeactivation.objects.filter(delete_at__isnull=True).order_by('-deactivated_at')
    return render(request, 'pool/deactivation.html', {'deactivations': deactivations})
